<template>
  <div class="hello">



        <el-button class="width100" type="primary">登陆</el-button>
 


    {{dirPath}}
    <h1>{{ msg }}1111</h1>
      <button @click="test111()">getDir</button>
      <button @click="openDialog()">OpenDialog</button>
      <button @click="getDir()">getDir</button>
      <button @click="readFile()">readFile</button>

       <button @click="testDb()">testDb</button>
      


  </div>
</template>

<script lang="ts">
import { defineComponent ,ref} from 'vue';

//import { Sequelize} from 'sequelize-typescript';
const { ipcRenderer, remote } = window.require('electron');
const Template = window.require('art-template');
const { Sequelize } = window.require('sequelize-typescript');
//const fse = window.require('fs-extra')
const fs = window.require('fs')

export default defineComponent({
  name: 'HelloWorld',
  props: {
    msg: String,
  },
  
  setup() {

    const dirPath = ref("")
    console.log("2222");

    console.log(__dirname);


function testDb(){

  const sequelize = new Sequelize("zytv", "community", "community2008", {

  host: "172.16.4.25",
  port: 1921,
  dialect: 'postgres',
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
    // dialectOptions: {
    //     useUTC: false //for reading from database
    // },   //非必须
  timezone: '+08:00'


  });


 sequelize.query("select * from pg_tables where schemaname = 'public'", { type: sequelize.QueryTypes.SELECT}).then((result: any) => {
            console.log('result:',result)
        });
 // const xx=sequelize.query("show tables");
  //console.log(xx);


  //const [results, metadata] = await sequelize.query("UPDATE users SET y = 42 WHERE x = 12");






  const dbpath= dirPath.value+"test.db";

  const sequelize2 = new Sequelize({

    dialect: 'sqlite',
    pool: {
      max: 5,
      min: 0,
      idle: 10000
    },
    storage: dbpath
  
    });
sequelize2.query("select * from project",{ type: sequelize.QueryTypes.SELECT}).then((result: any) => {
            console.log('result:',result)
        });










}

function readFile(){

    var data=fs.readFileSync('README.md', 'utf-8');
   console.log(data);


    var source = '<ul>'
+    '<% for (var i = 0; i < list.length; i ++) { %>'
+        '<li>索引 <%= i + 1 %> ：<%= list[i] %></li>'
+    '<% } %>'
+ '</ul>';

var render = Template.compile(source);
var html = render({list: ['摄影', '电影', '民谣', '旅行', '吉他']});

console.log(html);




}
    function getDir(){

fs.readdir('./', function(err:any, files:any){
    
    console.log(files)
})

    }

function openDialog(){
  console.log('dialog');
  ipcRenderer.send('open-directory-dialog');

  ipcRenderer.on('selected-directory', (event, path: Array<string>) => {
    console.log(path[0])
      if (path) {
        console.log(path);
      } else {
        console.log('项目路径错误');
      }
    });

}

    function test111(){

//
    ipcRenderer.send('get-root-path', '');
    ipcRenderer.on('reply-root-path', (event, arg: string) => {
      if (arg) {
        dirPath.value=arg;
        console.log(arg);
      } else {
        console.log('项目路径错误');
      }
    });

     // ipcRenderer.send('open-file-dialog');
     // dirPath.value="2222";
    // console.log('11111111111111');
     
    }

  return { //必须返回 模板中才能使用
   test111,dirPath,openDialog,getDir,readFile,testDb
  }

  }
});
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
